package topcbl.taxi;

import topcbl.data.FavoritePlace;
import topcbl.data.HistoryPlace;
import topcbl.util.GPSTracker;
import topcbl.util.StringUtils;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CallTaxiDialog extends Dialog {
	Context context;
	String address;
	FavoritePlace mFavoritePlace = null;
	HistoryPlace mHistoryPlace = null;
	// view
	TextView tvAddress;
	EditText etDetails, etNumber;
	Button btnBack, btnCallTaxi;
	// coordinate
	double lat, lng;

	public CallTaxiDialog(Context context, double lat, double lng) {
		super(context, R.style.MyDialogTheme);
		this.context = context;
		this.lat = lat;
		this.lng = lng;
		address = StringUtils.eliminateNumeral(GPSTracker
				.getAddressFromCoordinate(lat, lng, context));
	}

	public CallTaxiDialog(Context context, FavoritePlace place) {
		super(context, R.style.MyDialogTheme);
		this.context = context;
		this.mFavoritePlace = place;
		lat = place.getLat();
		lng = place.getLng();
	}

	public CallTaxiDialog(Context context, HistoryPlace place) {
		super(context, R.style.MyDialogTheme);
		this.context = context;
		this.mHistoryPlace = place;
		lat = place.getLat();
		lng = place.getLng();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// animation
		getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.dialog_calltaxi);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.dialog_title_bar_calltaxi);
		// change title divider
		int titleDividerId = context.getResources().getIdentifier(
				"titleDivider", "id", "android");
		View titleDivider = getWindow().getDecorView().findViewById(
				titleDividerId);
		titleDivider.setBackgroundColor(context.getResources().getColor(
				R.color.orange_trans)); // change divider color
		// init
		tvAddress = (TextView) findViewById(R.id.dialog_calltaxi_address);
		etDetails = (EditText) findViewById(R.id.dialog_calltaxi_details);
		etNumber = (EditText) findViewById(R.id.dialog_calltaxi_number);
		btnBack = (Button) findViewById(R.id.dialog_calltaxi_btnBack);
		btnCallTaxi = (Button) findViewById(R.id.dialog_calltaxi_btnCallTaxi);
		// setup
		if (mFavoritePlace != null)
			setupIfCallFromFavorite();
		else if (mHistoryPlace != null)
			setupIfCallFromHistory();
		if (address != null) {
			tvAddress.setText(address);
		} else {
			tvAddress.setText(context.getString(R.string.searchMessage) + " "
					+ String.format("%.3f : %.3f", lat, lng));
		}
		btnBack.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		btnCallTaxi.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String number = etNumber.getText().toString();
				String des = etDetails.getText().toString();
				if (number.length() > 0 || des.length() > 0) {
					String message = address + etNumber.getText()
							+ etDetails.getText();
					Intent callTaxiIntent = new Intent(context,
							CallTaxiActivity.class);
					callTaxiIntent.putExtra("message", message);
					callTaxiIntent.putExtra("lat", lat);
					callTaxiIntent.putExtra("lng", lng);
					context.startActivity(callTaxiIntent);
					// save in history
					MainActivity.myDatabase.addHistory(address,
							Integer.parseInt(number), des, lat, lng);
				} else {
					Toast.makeText(
							context,
							getContext().getString(R.string.toastCallTaxiError),
							Toast.LENGTH_SHORT).show();
				}
				dismiss();
			}
		});

	}

	private void setupIfCallFromFavorite() {
		address = mFavoritePlace.getAddress();
		etNumber.setText(mFavoritePlace.getNumber() + "");
		etDetails.setText(mFavoritePlace.getDescription());
	}

	private void setupIfCallFromHistory() {
		address = mHistoryPlace.getAddress();
		etNumber.setText(mHistoryPlace.getNumber() + "");
		etDetails.setText(mHistoryPlace.getDescription());
	}
}
